#include "stm32f10x.h"                  // Device header

uint16_t Num;

void Time_Init()
{
	//开启ABP1上的TIM3的时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	//为TIM3设为内部时钟
	TIM_InternalClockConfig(TIM3);
	//设置时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;      //滤波
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;	//从零开始加到设定值
	TIM_TimeBaseInitStruct.TIM_Period = 100 - 1;									//计数器计到一百是置中断
	TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;							//7200分频
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;							//关闭重复计数器
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);							//他会生成一个更新事件,使更新中断发生重合从而使Num加1
	
	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	//使能中断
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
	//设置NVIC
	//NVIC优先级分组 NVIC_PriorityGroup_2:抢占优先级与响应优先级都是两位
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	//NVIC初始化
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;									//设置TIM的使能和失能
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;									//使能
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;				//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;								//相应优先级
	NVIC_Init(&NVIC_InitStruct);
	
	TIM_SetCounter(TIM3,0);
	
	TIM_Cmd(TIM3, ENABLE);																				//开启定时器
	
}

uint16_t GetNum()
{
	return Num;
}


/*
TIM3中断函数
void TIM3_IRQHandler()
{
	if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
	{
		Num++;
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	}
	
}
*/
